package com.yuke.cloud.service.cmc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.smc.dto.ShopInfoDto;
import com.yuke.cloud.service.api.smc.service.SmcFeignApi;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.cmc.dao.CmsCouponMapper;
import com.yuke.cloud.service.cmc.entity.CmsCoupon;
import com.yuke.cloud.service.cmc.service.CmsCouponService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;


/**
 * Created by wg on 2019/09/27.
 */
@Service
@Transactional
public class CmsCouponServiceImpl extends BaseService<CmsCoupon> implements CmsCouponService {
    @Resource
    private CmsCouponMapper cmsCouponMapper;

    @Resource
    private UacUserFeignApi uacUserFeignApi;

    @Resource
    private SmcFeignApi smcFeignApi;

    @Override
    public List<CmsCoupon> getCouponList(Long userId,Integer sendAspect,Long groupId){
        //判断是否商家用户
        Long receiveId = 0L;

        Wrapper<UserInfoDto> userInfoWrapper= uacUserFeignApi.getUserInfoByUserId(userId);

        if (userInfoWrapper != null && userInfoWrapper.getResult() != null){
            if (sendAspect.equals(1)){

                List<ShopInfoDto>  shopInfoDto = smcFeignApi.getShopInfoDtoByUserId(userId);

                if (shopInfoDto!=null && shopInfoDto.size()>0){
                    receiveId = shopInfoDto.get(0).getShopId();
                }
            }else{
                receiveId = userId;
            }
        }

        return cmsCouponMapper.getCouponList(receiveId,sendAspect,groupId);
    }

    @Override
    public void getDeleteCouponId(Long couponId) {
        cmsCouponMapper.getDeleteCouponId(couponId);
    }

    @Override
    public List<CmsCoupon> getRedCouponlist(CmsCoupon cmsCoupon){
        Example example = new Example(CmsCoupon.class);
        Example.Criteria criteria = example.createCriteria();
        //criteria.andEqualTo("groupId",cmsCoupon.getCouponId());
        criteria.andCondition("send_type in (7,8,9,10)");
        List<CmsCoupon> cmsCouponList = cmsCouponMapper.selectByExample(example);
        return cmsCouponList;
    }
}
